<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2023-07-22 10:30:27
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Repositories;

use App\Models\Material;

use Illuminate\Support\Facades\DB;

class MaterialRepository extends Repositories
    {


    protected $relation_models = [
        'creator',
        'updator',
        'deletor',
        'unit',
        'uunit',
        'ounit',
        'unit1',
        'status',
        'material_nature',
        'film_category',
        'category'
    ];
    public function boot(Material $model)
        {
        $this->model = $model;
        }

    public function condition($request = null, $withauth = true)
        {
        if ($request->has("filter")) {
            $filters = $request->filter;
            foreach ($filters ?? [] as $keys => $filter) {
                foreach ($filter ?? [] as $key => $value)
                    if ($key == 'in_craftIds') {
                        foreach ($value ?? [] as $v) {
                            $this->orWhere("craftIds", "like", "%{$v}%");
                            }
                        unset($filters[$keys]['in_craftIds']);
                        }
                }

            $request->offsetSet("filter", $filters);
            }

        return parent::condition($request, $withauth);
        }
    public function fuzzySearch($value)
        {
        $value = trim($value);
        $this->where(function ($query) use ($value)
            {
            $query->orWhere("fullname", "like", "%{$value}%");
            $query->orWhere("spec", "like", "%{$value}%");
            $query->orWhere("code", "like", "%{$value}%");
            $query->orWhere("remark", "like", "%{$value}%");
            });
        }
    public function delete($ids)
        {
        return $this->whereIn("id", $ids)->get()->filter(function ($v)
            {
            return $v->delete();
            });
        }
    }